home *** CD-ROM | disk | FTP | other *** search
- Path: Grouper.Exis.Net!usenet
- From: tlwrnce@exis.net (Tim Lawrence)
- Newsgroups: comp.lang.c++
- Subject: Re: BC 3.1 - 4.5 (error!?)
- Date: 18 Apr 1996 13:39:45 GMT
- Organization: Exchange Information Systems Networks
- Message-ID: <4l5gn1$gtb@Grouper.Exis.Net>
- References: <3173D628.48F6@demos.su>
- NNTP-Posting-Host: ppp-2-52.exis.net
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.11
-
- Tested in BC 4.5 and 5.0. Results where what I expected. The reason you
- may think you are getting unpredictable results is the use of a static var
- in a function.
- In article <3173D628.48F6@demos.su>, 00alex@demos.su says...
- >
- >Hi, All
- >
- >Here is simple example of abnormal programming.
- >But BC crashed (sorry, it works and even compiles
- >the program, but it is unpredictable what will occur)
- >on it:
- >
- >#include <stdio.h>
- >
- >int st_func( int a )
- >{
- > static int p=a;
- ^^^ Stores a to p first time function used only!
- > return p++;
- // Because p is static it will return p increment it and then
- // use p+1 for p next time st_func is called
- >}
- >
- >int main( int argc, char *argv[] )
- >{
- > int i;
- > if( argc != 1 )
- > {
- > for( i = 10; i>0; i-- )
- > printf( "%d\n", st_func( i ) );
- // First time st_func is called p will be set to 10 so output will be as
- // follows: 10 11 12 13 14 15 16 17 18 19
- // NOTE: The parameter for st_func has meaning for the first time st_func
- // is called ONLY!
- > }
- > else
- > {
- > for( i = 0; i<10; i++ )
- > printf( "%d\n", st_func( i ) );
- > }
- > return 0;
- >}
- >
- >Enjoy...
- >
- >If you know what is wrong reply please :)
-
-